package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public List<DishItemVO> getDishItemById(Long id) {

        //根据套餐id查询出所有 SetmealDish
        List<SetmealDish>setmealDishList=setmealDishMapper.listByDishId(id);

        //根据dish查询出所有的DishItemVo
        List<DishItemVO>dishItemVOList=new ArrayList<>();

        for (SetmealDish setmealDish : setmealDishList) {
            DishItemVO dishItemVO=new DishItemVO();
            dishItemVO.setCopies(setmealDish.getCopies());
            Dish dish=dishMapper.selectById(setmealDish.getDishId());
            BeanUtils.copyProperties(dish,dishItemVO);
            dishItemVOList.add(dishItemVO);
        }

        return dishItemVOList;
    }

    @Override
    public List<Setmeal> selectSetmealListConditionBy(Long categoryId, Integer enable) {
        List<Setmeal> setmealList = setmealMapper.selectConditionBy(categoryId, enable);
        return setmealList;
    }

    @Override
    public PageResult selectPage(SetmealPageQueryDTO setmealPageQueryDTO) {

        //开启分页查询
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());

        Page<SetmealVO>page =setmealMapper.selectSetmealListBySetmealPageQueryDTO(setmealPageQueryDTO);
        List<SetmealVO> result = page.getResult();
        for (SetmealVO setmealVO : result) {
            setmealVO.setCategoryName(categoryMapper.selectNameById(setmealVO.getCategoryId()));
        }
        return new PageResult(page.getTotal(), result);
    }

        @Override
    public void addSetmeal(SetmealDTO setmealDTO) {
        //添加套餐
        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.insert(setmeal);

        for (SetmealDish setmealDish : setmealDTO.getSetmealDishes()) {
            setmealDish.setSetmealId(setmeal.getId());
        }
        //关联套餐与具体的菜品
        setmealDishMapper.insert(setmealDTO.getSetmealDishes());
    }

    @Override
    public void startOrStop(Long id, Integer status) {
        Setmeal setmeal=setmealMapper.selectById(id);
        setmeal.setStatus(status);
        setmealMapper.update(setmeal);
    }

    @Override
    public SetmealVO selectById(Long id) {
        Setmeal setmeal = setmealMapper.selectById(id);
        List<SetmealDish> setmealDishList=setmealDishMapper.listBySetmealId(id);
        SetmealVO setmealVO=new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        setmealVO.setSetmealDishes(setmealDishList);
        return setmealVO;
    }

    @Override
    public void updateSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.update(setmeal);
        setmealDishMapper.deleteBatchBySetmealId(setmealDTO.getId());
        for (SetmealDish setmealDish : setmealDTO.getSetmealDishes()) {
            setmealDish.setSetmealId(setmealDTO.getId());
        }
        setmealDishMapper.insert(setmealDTO.getSetmealDishes());
    }

    @Override
    public void deleteBatch(List<Long> ids) {
        //根据ids删除所有套餐本身
        setmealMapper.deleteBatch(ids);

        //根据ids删除所有套餐内关联的菜品从setmeal_dish表中
        for (Long id : ids) {
            setmealDishMapper.deleteBatchBySetmealId(id);
        }

    }
}
